class Solution {
    int row, col;

    void dfs(vector<vector<int>>& grid, int m, int n, int& nums)
    {
        if (m < 0 || n < 0 || m >= row || n >= col || grid[m][n] != 1)
            return;

        grid[m][n] = 2;
        nums++;
        dfs(grid, m - 1, n, nums);    //上
        dfs(grid, m, n - 1, nums);    //左
        dfs(grid, m + 1, n, nums);    //下
        dfs(grid, m, n + 1, nums);    //右
    }
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int ret = 0;
        row = grid.size();
        col = grid[0].size();
        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++)
                if (grid[i][j] == 1)
                {
                    int nums = 0;
                    dfs(grid, i, j, nums);
                    ret = max(ret, nums);
                }

        return ret;
    }
};